package com.emex.app.web.service;

import com.emex.app.shared.data.Client;
import com.emex.app.shared.data.ClientExample;
import com.emex.app.shared.persistance.ClientMapper;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Sergey Karavaev
 */
@Service
@Transactional
public class ClientService {

    @Autowired
    private ClientMapper clientMapper;

    public int deleteByPrimaryKey(Integer id) {
        return clientMapper.deleteByPrimaryKey(id);
    }

    public Client insert(Client record) {
        clientMapper.insert(record);
        return record;
    }

    public List<Client> selectByExample(ClientExample clientExample) {
        return clientMapper.selectByExample(clientExample);
    }

    public Client selectByPrimaryKey(Integer id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    public Client updateByPrimaryKey(Client record) {
        clientMapper.updateByPrimaryKey(record);
        return record;
    }

    public List<Client> getAllClients() {
        return selectByExample(null);
    }

    public List<Client> selectByPhoneOrSurname(String criteria) {
        ClientExample clientEx = new ClientExample();
        if (Character.isDigit(criteria.charAt(0)) || criteria.startsWith("+")) {
            clientEx.createCriteria().andPhonesLike(criteria);
        } else {
            clientEx.createCriteria().andSurnameLike(criteria);
        }
        
        return selectByExample(clientEx);
    }
}
